package model.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import tools.excel.tool;
import constant.constant;
import dom.domain.Examplace;
import dom.domain.ExamplaceDAO;
import dom.domain.Expert;
import dom.domain.ExpertAllocateDAO;
import dom.domain.ExpertDAO;
public class ExpertService {
	private ExpertDAO expertDao;
	private ExamplaceDAO epDao;
	private ExpertAllocateDAO eaDao;
	public void setExpertDao(ExpertDAO expertDao) {
		this.expertDao = expertDao;
	}
	public void setEpDao(ExamplaceDAO epDao) {
		this.epDao = epDao;
	}
	public void setEaDao(ExpertAllocateDAO eaDao) {
		this.eaDao = eaDao;
	}
	private Map<String, String> map;

	public String addExpert(String expertInfo) {
		map = new HashMap<String, String>();
		try {
			JSONObject jexpert = JSONObject.fromObject(expertInfo);
			if (!jexpert.containsKey("expertId")) {
				map.put("flag", "false");
				map.put("info", "expertId is null");
				return JSONObject.fromObject(map).toString();
			}
			Expert temp = expertDao.findById(jexpert.getString("expertId"));
			if(temp!=null)
			{
				map.put("flag", "false");
				map.put("Info", "expertId已存在");
				return JSONObject.fromObject(map).toString();
			}
			if (!jexpert.containsKey("expertName")) {
				map.put("flag", "false");
				map.put("info", "expertId is null");
				return JSONObject.fromObject(map).toString();
			}
			if (!jexpert.containsKey("email")) {
				map.put("flag", "false");
				map.put("info", "expertId is null");
				return JSONObject.fromObject(map).toString();
			}
			Expert expert = new Expert();
			expert.setExpertId(jexpert.getString("expertId"));
			expert.setExpertName(jexpert.getString("expertName"));
			expert.setEmail(jexpert.getString("email"));
			if(jexpert.containsKey("epId"))
			{
				expert.setEpId(Integer.parseInt(jexpert.getString("epId")));
			}
			expert.setPassword(tool.getPassword());
			expertDao.save(expert);
			map.put("flag", "true");
			map.put("info", "add success");
		} catch (JSONException e) {
			map.put("flag", "false");
			map.put("info", e.getMessage());
		}
		return JSONObject.fromObject(map).toString();
	}
	public String delete(String expertId) {
		map = new HashMap<String, String>();
		if(expertId == null)
		{
			map.put("flag", "false");
			map.put("Info", "专家ID为空");
			return JSONObject.fromObject(map).toString();
		}
		Expert tempExpert = expertDao.findById(expertId);
		if (tempExpert == null) {
			map.put("flag", "false");
			map.put("info", "expertId不存在");
		} else {
			expertDao.delete(tempExpert);
			map.put("flag", "true");
			map.put("info", "删除成功");
		}
		return JSONObject.fromObject(map).toString();
	}
	public String allocationAll()
	{
		List expertList = expertDao.findAll();
		List epList = epDao.findAll();
		map = new HashMap<String,String>();
		int n = 0;
		int count = 0;
		for(int j = 0;j<epList.size();j++)
		{
			Examplace ep = (Examplace) epList.get(j);
			count+=ep.getEpClassNo();
		}
		if(count*3>expertList.size())
		{
			map.put("flag", "false");
			map.put("Info","专家人数不够");
			return JSONObject.fromObject(map).toString();
		}
		for(int i=0;i<epList.size();i++)
		{
			Examplace ep = (Examplace) epList.get(i);
			int number = ep.getEpClassNo();
			for(int m=0;m<number;m++)
			{
				for(int j = 0; j < 3; j++)
				{
					if(n<expertList.size())
					{
						Expert expert = (Expert) expertList.get(n);
						expert.setEpId(ep.getEpId());
						expert.setEpClassId(m);
						expertDao.attachDirty(expert);
						n++;
					}
				}
			}
		}

		map.put("flag", "true");
		map.put("Info","sucess");
		return JSONObject.fromObject(map).toString();
	}
	
	public String allocationPsd(String expertId) {
		map = new HashMap<String, String>();
		Expert expert = expertDao.findById(expertId);
		expert.setPassword(tool.getPassword());
		expertDao.attachDirty(expert);
		map.put("flag", "true");
		map.put("Info", "success");
		//send mail
		return JSONObject.fromObject(map).toString();
	}
	public String  deleteAll(String expertIds)
	{
		if(expertIds==null)
		{
			map.put("flag", "false");
			map.put("info", "expertIds为空");
			return JSONObject.fromObject(map).toString();
		}
		String[] experts = expertIds.split(",");
		StringBuilder Info = new StringBuilder();
		for(int i = 0; i < experts.length; i++)
		{
			JSONObject result = JSONObject.fromObject(delete(experts[i]));
			if(result.getString("flag").equals("false"))
			{
				Info.append(result.getString("Info"));
			}
		}
		map.put("flag","true");
		map.put("Info",Info.toString());
		return JSONObject.fromObject(map).toString();
	}
	public String getList(int pageNum){
		map = new HashMap<String,String>();
		JSONArray array = new JSONArray();
		List list = expertDao.getList(pageNum, 10);
		for(int i =0;i<list.size();i++)
		{
			Expert expert = (Expert)list.get(i);
			JSONObject jInfo = JSONObject.fromObject(expert.toString());
			if(expert.getEpId()!=null)
			{
				Examplace ep = epDao.findById(expert.getEpId());
				jInfo.put("epname", ep.getEpname());
			}else
			{
				jInfo.put("epname", "");
			}
			array.add(jInfo);
		}
		if(array.size()==0)
		{
			map.put("flag", "false");
			map.put("Info", "未添加任何数据");
		}
		else
		{
			map.put("flag", "true");
			map.put("Info","success");
			map.put("array", array.toString());
		}
		return JSONObject.fromObject(map).toString();
	}
	public String modify(String expertInfo) {
		map = new HashMap<String, String>();
		try {
			JSONObject jexpert = JSONObject.fromObject(expertInfo);
			if (!jexpert.containsKey("expertId")) {
				map.put("flag", "false");
				map.put("info", "expert is empty");
				return JSONObject.fromObject(map).toString();
			}
			Expert tempExpert = expertDao.findById(jexpert
					.getString("expertId"));
			if (tempExpert == null) {
				map.put("flag", "false");
				map.put("info", "expertId不存在");
				return JSONObject.fromObject(map).toString();
			}
			if (jexpert.containsKey("expertName")) {
				tempExpert.setExpertName(jexpert.getString("expertName"));
			}
			if (jexpert.containsKey("email")) {
				tempExpert.setEmail(jexpert.getString("email"));
			}
			if (jexpert.containsKey("password")) {
				tempExpert.setPassword(jexpert.getString("password"));
			}
			if(jexpert.containsKey("epId"))
			{
				if(tempExpert.getEpClassId()!=null)
				{
					map.put("flag", "false");
					map.put("info", "已分配EpClassId,无法修改");
					return JSONObject.fromObject(map).toString();
				}
				if(tempExpert.getEpId()==null&&!jexpert.getString("epId").equals(""))
					tempExpert.setEpId(Integer.parseInt(jexpert.getString("epId")));
			}
			expertDao.attachDirty(tempExpert);
			map.put("flag", "true");
			map.put("info", "修改成功");
		} catch (JSONException e) {
			map.put("flag", "false");
			map.put("info", e.getMessage());
		}
		return JSONObject.fromObject(map).toString();
	}

	public String query(String expertName) {
		map = new HashMap<String, String>();
		 
		if(expertName==null)
		{
			map.put("flag", "false");
			map.put("Info", "专家姓名为空");
			return JSONObject.fromObject(map).toString();
		}
		List list = expertDao.query(expertName);
		JSONArray array = new JSONArray();
		for(int i =0;i<list.size();i++)
		{
			Expert expert = (Expert)list.get(i);
			JSONObject jInfo = JSONObject.fromObject(expert.toString());
			if(expert.getEpId()!=null)
			{
				Examplace ep = epDao.findById(expert.getEpId());
				jInfo.put("epname", ep.getEpname());
			}
			else
			{
				jInfo.put("epname", "");
			}
			array.add(jInfo);
		}
		if(array.size()==0)
		{
			map.put("flag", "false");
			map.put("Info", "未添加任何数据");
		}
		else
		{
			map.put("flag", "true");
			map.put("Info","success");
			map.put("array", array.toString());
		}
		return JSONObject.fromObject(map).toString();
	}
}
